package cn.itcast.itcaststore.web.servlet.manager;

import cn.itcast.itcaststore.domain.Products;
import cn.itcast.itcaststore.exception.ListProductException;
import cn.itcast.itcaststore.serive.ProductService;
import cn.itcast.itcaststore.serive.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 后台系统
 * 查询所有商品信息的servlet
 */
@WebServlet("/admin/listProduct")
public class ListProductServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1.创建service层的对象
        ProductService service = new ProductServiceImpl();
        try {
            // 2.调用service层用户查询所有商品的listAll()方法
            List<Products> ps = service.listAll();
            // 3.将查询到的所有商品放进request域中
            request.setAttribute("ps",ps);
            // 4.请求转发到list.jsp
            request.getRequestDispatcher("/admin/products/list.jsp").forward(request,response);
        }catch (ListProductException e){
            e.printStackTrace();
            response.getWriter().write(e.getMessage());
            return;
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
